package librus.codebase.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;

import librus.codebase.util.FileUtil;

public class Processor extends Thread 
{

	public Processor(String path, Boolean resize, Boolean dither, JLabel progress, JButton runBtn)
	{
		super();
		
		__path = path;
		__resize = resize;
		__dither = dither;
		__progress = progress;
		__runBtn = runBtn;		
	}
	
	private String  __path;	
	private Boolean __resize;
	private Boolean __dither;
	private JLabel  __progress;
	private JButton __runBtn;
	
	private File __sourceDir;
	private File __targetDir;
	
	private int __total;
	private int __processed;
	private int __errorCount;
	
	
	public void processAll()
	{
		if (!createDir()) return;		
		
		__runBtn.setEnabled(false);
		File[] list = getFiles();
		__total = list.length*4;
		__processed = 0;
		__errorCount = 0;
		
		ImageProcessor imgProcessor = new ImageProcessor();
		
		for (File file : list) 
		{				
			try 
			{
				processFile(file, imgProcessor);
				
			} 
			catch (IOException e) 
			{
				System.out.println(e.getMessage());
				__errorCount++;
			}				
		}		
		
		__progress.setText("Complete with "+__errorCount+" errors!");
		__runBtn.setEnabled(true);
	}
	
	private void processFile(File f, ImageProcessor imgProcessor) throws IOException
	{		
		BufferedImage image = ImageIO.read(f);	
		String path =  __targetDir.getAbsolutePath()+File.separator;
		String[] name = f.getName().split("\\.");
		
		if (image.getWidth() > image.getHeight())
		{			
			WideImage wideImage = imgProcessor.slice(image);
			updateProgress(++__processed);
			
			processImage(wideImage.right, imgProcessor, path+name[0]);
			processImage(wideImage.left , imgProcessor, path+name[0]+"_");			
		}
		else
		{
			updateProgress(++__processed);
			processImage(image, imgProcessor,path+name[0]);
		}
	}
	
	private void processImage(BufferedImage img, ImageProcessor proc, String path) throws IOException
	{
		if (__resize)
			img = proc.resize(img, 824, 1200);
		
		updateProgress(++__processed);
		
		if (__dither)
			img = proc.quantize(img);
		
		updateProgress(++__processed);
		
		proc.saveImage(img, path);
		
		updateProgress(++__processed);
	}
		
	private Boolean createDir()
	{
		__sourceDir = new File(__path);
		if (!__sourceDir.exists() || !__sourceDir.isDirectory())
		{
			__progress.setText("Directory doesn't exist");
			return false;
		}
		
		__targetDir = new File(__path+"-export");
		
		if (__targetDir.exists())
			__targetDir.delete();
		
		Boolean isCreated = __targetDir.mkdir();
		
		if (!isCreated)
			__progress.setText("Can't create dir");
		
		return isCreated;
	}
	
	public void run() 
	{
		processAll();
	}
	
	private File[] getFiles()
	{
		FilenameFilter filter = new FilenameFilter() 
		{			
			public boolean accept(File dir, String name) 
			{
				return FileUtil.isComicFile(name); 
			}
		};
		
		return __sourceDir.listFiles(filter);
	}
	
	private void updateProgress(int progress)
	{
		int pro = (progress*100)/__total;
		__progress.setText("Progress: "+(pro)+"%");
	}
}
